<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	$item=$this->plugin_setting('setting');
	$return['adv']=$item['hdps'];
	$return['setting']=$item;
	$info=pdo_get('jy_cmso2o_list',array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid']));
	if(!$info){
		exi('您还未入驻，点确定跳转到入驻界面','error','check');
	}
	if($info['status']==0){
		exi('您的申请正在审核，请耐心等待','error','index/');
	}
	$return['info']=$info;
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'status'=>1);
	$return['pcates']=pdo_getall('jy_cmso2o_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	$return['op']='main';
	exi($return);
}
if($_RQ['op']=='check'){
	$return['op']='check';
	exi($return);
}
if($_RQ['op']=='ruzhu'){
	$setting=$this->plugin_setting('setting');
	
	if($setting['status']==0){
		exi('未开启入驻','error');
	}
  $group=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
  $return['info']=array('address'=>'');
  $return['op']='ruzhu';
  $return['group']=$group;
  $groupspram=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'status'=>1);
  $return['pcates']=pdo_getall('jy_cmso2o_group',$groupspram,array('title','id'),'id');
  foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
  }
  exi($return);
}
if($_RQ['op']=='ruzhupost'){
	$setting=$this->plugin_setting('setting');
	
	if($setting['status']==0){
		exi('未开启入驻','error');
	}
	$group=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	$postdata=get_postdata();
	if(!$postdata['title']){
			exi('商户名称不能为空！','error');
	}
	if(!$postdata['logo']){
			exi('logo不能为空！','error');
	}
	if(!$postdata['phone']){
			exi('商户电话不能为空！','error');
	}
	if(!$postdata['address']){
			exi('商户地址不能为空！','error');
	}
	if(!$postdata['lat_lng']){
			exi('商户坐标不能为空！','error');
	}
	$data=array_fields(array('title','type','logo','bgcolor','bgimg','onlinetime','group_id','ccate','scate','url','image','lat_lng','address','price','phone','desc','hdp','ewm'),$postdata);
	$data['ishow']=1;
	$data['status']=0;
	$data['onlinetime']=json_encode($data['onlinetime']);
	$data['lat_lng']=json_encode($data['lat_lng']);
	$data['uniacid']=$_SESSION['uniacid'];
	$data['uid']=$_SESSION['uid'];
	$data['createtime']=TIMESTAMP;
	$_SESSION['o2o']['data']=$data;	
	if($setting['price']==0 || $_SESSION['os']=='wxapp'){
			$data['status']=1;
			pdo_insert('jy_cmso2o_list',$data);
			exi('申请成功！','','detail/');
	}else{
			$price=$setting['price'];
			$data1=array(
				'uniacid'=>$_SESSION['uniacid'],
				'status'=>0,
				'ordersn'=>get_ordersn('jy_cmso2o_order','ordersn'),
				'price'=>$price,
				'mid'=>$_SESSION['uid'],
				'createtime'=>TIMESTAMP		
			);
			pdo_insert('jy_cmso2o_order',$data1);
			$params['fee']     = $price;
			$params['title']   = 'CMSo2o商户入驻';
			$params['id']=pdo_insertid();
			$params['plugin']='jy_cmso2o';
			$params['back_url']='jy_cmso2o/merchants/payresult';
			cfc('core')->pay($params);
	}
}
if($_RQ['op']=='set'){
	$group=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'status'=>1));
	
	$info=pdo_get('jy_cmso2o_list',array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid']));
	if(empty($info['id'])){
			exi('您还未入驻店铺','error','jy_cmso2o/merchants/ruzhu/');
	}
	$info['onlinetime']=json_decode($info['onlinetime'],true);
	$info['lat_lng']=json_decode($info['lat_lng'],true);
	$return['info']=$info;
	$return['op']='set';
	$return['group']=$group;
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'status'=>1);
	$return['pcates']=pdo_getall('jy_cmso2o_group',$groupspram,array('title','id'),'id');
	foreach($return['pcates'] as &$pv){
		$pv['ccates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$pv['id']),array('title','id'),'id');
		foreach($pv['ccates'] as &$cv){
			$cv['scates']=pdo_getall('jy_cmso2o_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$cv['id']),array('title','id'),'id');
		}
	}
	exi($return);
}
if($_RQ['op']=='post'){	
	$postdata=get_postdata();
	$data=array_fields(array('title','type','logo','bgcolor','bgimg','onlinetime','group_id','ccate','scate','url','image','lat_lng','address','price','phone','desc','hdp','ewm'),$postdata);
	$data['onlinetime']=json_encode($data['onlinetime']);
	$data['lat_lng']=json_encode($data['lat_lng']);			
	pdo_update('jy_cmso2o_list',$data,array('uniacid'=>$_SESSION['uniacid'],'uid'=>$_SESSION['uid']));
	exi('设置成功！','','/merchants/');		
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];
	if($payresult['status']==1){
		$order=pdo_get('jy_cmso2o_order',array('id'=>$payresult['id']));
		if($order['price']==$payresult['fee']){
			pdo_update('jy_cmso2o_order',array('status'=>1,'pay_id'=>$payresult['pay_id']),array('id'=>$payresult['id']));
			$data=$_SESSION['o2o']['data'];
			$data['status']=1;
			pdo_insert('jy_cmso2o_list',$_SESSION['o2o']['data']);
			exi('支付成功','success','jy_cmso2o/index/');
		}else{
			exi('支付失败2','error','jy_cmso2o/index/');
		}
	}else{
		exi('支付失败1','error','jy_cmso2o/index/');
	}
}
?>